<?php
namespace App\Http\Controllers\Admin;
use App\Models\Admin\TvmMobileDetail;
use App\Models\Admin\TvmMobileCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\AdminAction;


class GroupController extends CommonController {

	public function __construct()
	{
		parent::__construct();
		$this->checkLogin();
	}


	/*
     * 管理员列表
     */
    public function groupList(Request $request){
    	$condition = array();
		$keywords = $request->input('keywords');
		if($keywords){
			$condition[] = ['title', 'like', '%'.$keywords.'%'];
		}
		$list = Db::table('auth_group')->where($condition)->orderBy('id','desc')->paginate(10);
		return view('admin.group.group_list',[
			'data' => $list,
			'keywords' => $keywords
		]);
    }

    /*
     * 添加权限菜单
     */
    public function groupAdd(Request $request){
    	$id = $request->input('id');
    	if($request->isMethod('post')){
    	   $title = $request->input('title');
			if(!$title){
				$this->ajax_error('用户组名不能为空');
			}

		    $where[] = ['title','=',$title];

			$userInfo = DB::table('auth_group')->where($where)->get();
			if(!$id){
				if(!$userInfo->isEmpty()){
				$this->ajax_error('用户组名已存在');
			}
			}
			

			$rules = $request->input('rules');


            if(!empty($rules)){
                $rules = implode(',',$rules).',';
            }
           
			$data = array(
				'id' => TvmMobileCategory::getNextSeq(),
				'title' => $title, //名称
				'status' => $request->input('status'), //状态
				'rules' => $rules, //规则ID

			);
			if(!$id){
				$result = Db::table('auth_group')->insert($data);
			}else{
				$data['id'] = $id;
				$result = DB::table('auth_group')->where('id',$id)->update($data);
			}
			
			if(!$result){
				$this->ajax_error('添加失败');
			}
			//record_action(AdminAction::getNextSeq(),'group','groupAdd','用户组信息添加',json_encode($_POST));
			$this->ajax_success('添加成功');


		
		}

		$condition = array();
		if($id){
			$condition[] = ['id','=',$id];
		}
		$condition[] = ['status','=',1];
		$condition[] = ['verify','=',1];
		$condition[] = ['is_show','=',1];
		$category = DB::table('auth_rule')->where($condition)->orderBy('sorts','asc')->get();
		$category = $this->getTree($category);
		// dd($category);exit;
		$group = array();
		$info = array();
		$id = '';
		return view('admin.group.group_add',[
			'category' => $category,
			'group' => $group,
		    'info' => $info,
		     'id' => $id,
			'action' => 'Add'
		]);

    }

     /**
	 * @param Request $request
	 * 编辑用户组数据
	 */
	public function groupEdit(Request $request){
		$condition = array();
		$id = $request->input('id');
		if($id){
			$condition[] = ['id','=',$id];
		}
		$group = DB::table('auth_group')->where($condition)->first();
		$condition1[] = ['status','=',1];
		$condition1[] = ['verify','=',1];
		$condition1[] = ['is_show','=',1];
		$info =array();
		if($group){
			 $info['rules'] = explode(',',$group->rules);
			 $category = DB::table('auth_rule')->where($condition1)->orderBy('sorts','asc')->get();
			 $category = $this->getTree($category);
		}
		//$result = DB::table('auth_group')->where('id',$id)->delete();
		// dd($category);exit;
		return view('admin.group.group_add',[
		'category' => $category,
		'group' => $group,
		'info' => $info,
		'id' => $id,
		'action' => 'Add'
		]);
	}

    /**
	 * @param Request $request
	 * 删除用户组数据
	 */
	public function groupDel(Request $request){
		$ids = $request->input('id');
		 if(!empty($ids)){
            $idData = explode(',',$ids);
            foreach($idData as $k=>$v){
                $result = DB::table('auth_group')->where('id',$v)->delete();
            }
           $this->ajax_success('删除成功',$result);
        } else {
            $this->ajax_error('删除失败',$result);
        }
	}

		
}